package com.coocaa.ops.admin.v1.dict.util;

import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.base.util.WebUtil;
import com.coocaa.ops.admin.v1.dict.service.IDictInfoQueryService;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserEntity;
import com.coocaa.ops.common.core.biz.dict.entity.DictInfoEntity;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author bijiahao
 * @date : 2019/9/17.
 * @description 数据字典工具类
 */
public class DictUtil {

    @Setter
    private static IDictInfoQueryService dictInfoQueryService;
    private static CommonLanguageProperties commonLanguageProperties;

    /**
     * 通过分类查询列表信息
     *
     * @param category
     * @return
     */
    public static List<DictInfoEntity> queryByCategory(String category) {
        return getDictInfoService().queryByCategory(category);
    }

    private static IDictInfoQueryService getDictInfoService() {
        Assert.notNull(dictInfoQueryService, "IDictInfoQueryService instance " + commonLanguageProperties.getNotNull() + " DictUtil-dictInfoQueryService");
        return dictInfoQueryService;
    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        DictUtil.commonLanguageProperties = commonLanguageProperties;
    }

    public static List<Integer> getDicts(HttpServletRequest request, Integer businessType) {
        List<Integer> dictList = new ArrayList<>();
        SysUserEntity user = WebUtil.getCurrentUser(request);
        // 有传相关业务组信息上来
        if (businessType != null) {
            dictList.add(businessType);
        } else {
            // 没有传相关业务组信息上来
            List<Integer> dicts = user.getDicts();
            if (CollectionUtils.isNotEmpty(dicts)) {
                dictList.addAll(dicts);
            }
        }
        return dictList;
    }
}
